<!--
* @Author: bln
* @Date:  2023-11-09 14:12:03
 * @FilePath: \zmglpt-ui-mobile-v1.1\dev\pages\check\approve\creditRepairAapply.zh-FJ.vue
* @Description: 市管-信用修复--福建 统一待办审批跳转页
-->

<template>
	<view class="page">
		<!-- #ifndef H5-DINGDING -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999"></theme-navbar>
		<!-- #endif -->
		<view class="content">
			<u-form ref="uForm" v-model="baseInfo" label-position="top" label-width="auto">
				<u-collapse ref="cons" @change="changeHeight()">
					<u-collapse-item>
						<template #title>
							<u-row>
								<u-col span="12" style="display: flex">
									<view style="
													width: 8rpx;
													height: 32rpx;
													background: #0079f2;
													opacity: 1;
													margin-left: -5px;
													border-radius: 4rpx;
													float: left;
													margin-right: 16rpx;
												"></view>
									<view style="
													height: 36rpx;
													line-height: 36rpx;
													text-align: center;
													float: left;
													font-weight: bold;
													font-size: 16px;
												">
										基础信息
									</view>
								</u-col>
							</u-row>
						</template>
						<u-form-item label="修复类型">
							<u-radio-group v-model="baseInfo.repairType">
								<u-radio v-for="item, index in dictList" :key="index" :label="item.label" :name="item.value"
									:disabled="true">{{ item.label }}</u-radio>
							</u-radio-group>
						</u-form-item>
						<u-form-item label="申请人">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.applicant"></u-input>
						</u-form-item>
						<u-form-item label="申请时间">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.applyDate"></u-input>
						</u-form-item>
						<u-form-item label="许可证号">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.licNo"></u-input>
						</u-form-item>
						<u-form-item label="企业（字号）名称">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.companyName"></u-input>
						</u-form-item>
						<u-form-item label="统一社会信用代码">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.businessLicNo"></u-input>
						</u-form-item>
						<u-form-item label="负责人">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.managerName"></u-input>
						</u-form-item>
						<u-form-item label="涉烟违法违规行为">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.targetName"></u-input>
						</u-form-item>
						<u-form-item label="扣分值">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.repairScore"></u-input>
						</u-form-item>
						<u-form-item label="信用等级">
							<u-input placeHolder="" type="text" :disabled="true"
								v-model="baseInfo.repairBeforeCreditLevel"></u-input>
						</u-form-item>
						<u-form-item label="信用分值">
							<u-input placeHolder="" type="text" :disabled="true"
								v-model="baseInfo.repairBeforeCreditScore"></u-input>
						</u-form-item>
						<u-form-item label="问题描述">
							<u-input placeHolder="" type="textarea" :disabled="true"
								v-model="baseInfo.problemDescription"></u-input>
						</u-form-item>
						<u-form-item label="核查情况">
							<u-input placeHolder="" type="textarea" :disabled="true"
								v-model="baseInfo.checkResult"></u-input>
						</u-form-item>
					</u-collapse-item>
				</u-collapse>
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<!-- 附件列表开始 -->
				<u-collapse ref="files" @change="changeHeight()">
					<u-collapse-item>
						<template #title>
							<u-row>
								<u-col span="12" style="display: flex">
									<view style="
											width: 8rpx;
											height: 32rpx;
											background: #0079f2;
											opacity: 1;
											margin-left: -5px;
											border-radius: 4rpx;
											float: left;
											margin-right: 16rpx;
										"></view>
									<view style="
											height: 36rpx;
											line-height: 36rpx;
											text-align: center;
											float: left;
											font-weight: bold;
											font-size: 16px;
										">
										附件列表
									</view>
								</u-col>
							</u-row>
						</template>
						<scroll-view :scroll-y="false" :scroll-x="true"
							:style="{ height: scrollRealHeight, marginTop: '5px', paddingBottom: '10px' }">
							<u-table class="tb-lay" ref="tbRef" style="width:100vw">
								<u-tr>
									<u-th width="10%">序列</u-th>
									<u-th width="55%">文件名</u-th>
									<u-th width="20%">大小</u-th>
									<u-th width="20%">状态</u-th>
								</u-tr>
								<u-tr v-for="(it, index) in dataSource" :key="'ds' + index">
									<u-td width="10%">{{ index + 1 }}</u-td>
									<u-td width="55%"><text class="td-text">{{ it.gfiStoreName || '&nbsp;' }}</text></u-td>
									<u-td width="20%"><text class="td-text">{{ it.gfiSize || '&nbsp;' }}</text></u-td>
									<u-td width="20%"><text class="td-text">上传成功</text></u-td>
								</u-tr>
							</u-table>
						</scroll-view>
					</u-collapse-item>
				</u-collapse>
				<!-- 附件列表结束 -->

				<!-- 承办人意见 -->
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<form-section title="承办人意见"></form-section>
				<u-form-item label="">
					<u-input placeHolder="" type="textarea" :disabled="undertakerIsEdit"
						v-model="baseInfo.handlePersonAdvice"></u-input>
				</u-form-item>
				<u-form-item label="签名">
					<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.handlePerson"></u-input>
				</u-form-item>
				<u-form-item label="承办时间">
					<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.applyDate"></u-input>
				</u-form-item>

				<!-- 承办部门意见 -->
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<form-section title="承办部门意见"></form-section>
				<u-form-item label="">
					<u-radio-group v-model="departmentParams.approveMemoResult" @change="handleChangeRadio"
						:disabled="departmentIsEdit">
						<u-radio v-for="item, index in approveList" :key="index" :label="item.label" :name="item.value">
							{{ item.label }}</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="">
					<u-input placeHolder="" type="textarea" :disabled="departmentIsEdit"
						v-model="departmentParams.approveMemo"></u-input>
				</u-form-item>
				<u-form-item label="签名">
					<u-input placeHolder="" type="text" :disabled="true" v-model="departmentParams.handlePerson"></u-input>
				</u-form-item>
				<u-form-item label="承办时间">
					<u-input placeHolder="" type="text" :disabled="true" v-model="departmentParams.applyDate"></u-input>
				</u-form-item>

				<!-- 负责人意见 -->
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<form-section title="负责人意见"></form-section>
				<u-form-item label="">
					<u-radio-group v-model="responsibleParams.approveMemoResult" @change="handleChangeRadioRe"
						:disabled="responsibleIsEdit">
						<u-radio v-for="item, index in responsibleList" :key="index" :label="item.label" :name="item.value">
							{{ item.label }}</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="">
					<u-input placeHolder="" type="textarea" :disabled="responsibleIsEdit"
						v-model="responsibleParams.approveMemo"></u-input>
				</u-form-item>
				<u-form-item label="签名">
					<u-input placeHolder="" type="text" :disabled="true" v-model="responsibleParams.handlePerson"></u-input>
				</u-form-item>
				<u-form-item label="承办时间">
					<u-input placeHolder="" type="text" :disabled="true" v-model="responsibleParams.applyDate"></u-input>
				</u-form-item>

				<!-- 副局长意见 -->
				<view v-if="baseInfo.repairType == '04'">
					<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
						:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
					<form-section title="副局长意见"></form-section>
					<u-form-item label="">
						<u-radio-group v-model="auxiliaryParams.approveMemoResult" @change="handleChangeRadioAu"
							:disabled="auxiliaryIsEdit">
							<u-radio v-for="item, index in responsibleList" :key="index" :label="item.label"
								:name="item.value">
								{{ item.label }}</u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item label="">
						<u-input placeHolder="" type="textarea" :disabled="auxiliaryIsEdit"
							v-model="auxiliaryParams.approveMemo"></u-input>
					</u-form-item>
					<u-form-item label="签名">
						<u-input placeHolder="" type="text" :disabled="true"
							v-model="auxiliaryParams.handlePerson"></u-input>
					</u-form-item>
					<u-form-item label="承办时间">
						<u-input placeHolder="" type="text" :disabled="true" v-model="auxiliaryParams.applyDate"></u-input>
					</u-form-item>
				</view>


				<!-- 审批历史开始 -->
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<form-section title="审批历史"></form-section>
				<u-form-item label="" class="u-m-l-12">
					<flow-his-no-doc :bizUuid="prvParam.bizUuid" :processDefKey="prvParam.gfCode"
						:endTitle="endTitle"></flow-his-no-doc>
				</u-form-item>
				<!-- 审批历史结束 -->
			</u-form>
		</view>
		<view class="submit-btns btn-other-cls" v-if="ifSub">
			<u-button class="u-btn" type="primary" @click="handleRelease">提交</u-button>
		</view>
		<view class="submit-btns btn-other-cls" v-if="!approvalDisabled && !ifSub && !approveDis">
			<u-button class="u-btn" type="error" @click="handleChange('03')">退回修改</u-button>
			<u-button class="u-btn" type="primary" @click="submit">审批通过</u-button>
		</view>
	</view>
</template>

<script>
import DicChoice from '@/components/dic_choice.vue';
import formSection from '@/components/form-section.vue';
import creditService from '@/service/check/check.service.js';
import commonService from "@/service/common/common.service.js";
import {
	M_DAY_PLAN_STATUS
} from '@/service/check/mapping/enum.js';
import FlowHisNoDoc from '@/components/flow-his-no-doc.vue';
export default {
	components: {
		formSection,
		FlowHisNoDoc,
		DicChoice
	},
	onLoad() {
		this.prvParam = this.getRouteParam() || {};
		console.log(this.prvParam)
		
		this.initData();
	},
	data() {
		return {
			prvParam: {},
			baseInfo: {},
			dataSource: [],
			scrollRealHeight: '50px',
			bean: {},
			rules: {
				applyReason: [{
					required: true,
					message: '请选择终止原因',
					// 可以单个或者同时写两个触发验证方式
					trigger: ['change', 'blur'],
				},],
			},
			dictList: [{
				label: '依申请修复',
				value: '02'
			}, {
				label: '异议修复',
				value: '03'
			}, {
				label: '特殊修复',
				value: '04'
			}],
			responsibleList: [{
				label: '准予修复',
				value: '01'
			}, {
				label: '不予修复',
				value: '02'
			}],
			step: '',
			endTitle: '',
			approvalDisabled: false,
			undertakerIsEdit: true,
			departmentIsEdit: true,
			responsibleIsEdit: true,
			auxiliaryIsEdit: true,
			responsibleParams: {
				approveMemoResult: '01',
				approveMemo: '准予修复',
				handlePerson: ' ',
				applyDate: ' '
			},
			departmentParams: {
				approveMemoResult: '01',
				approveMemo: '同意',
				handlePerson: ' ',
				applyDate: ' '
			},
			auxiliaryParams: {
				approveMemoResult: '01',
				approveMemo: '准予修复',
				handlePerson: ' ',
				applyDate: ' '
			},
			approveDis: true,
			ifSub: false,
			approveList: [{
				label: '同意',
				value: '01'
			}, {
				label: '不同意',
				value: '02'
			}]
		};
	},
	methods: {
		handleRelease() {
			if (!this.baseInfo.handlePersonAdvice) {
				return this.$u.toast('请输入承办人意见');
			}
			this.toSelectPerson();
		},
		submit() {
			this.$modal.showLoading('请稍后...');
			creditService.getFlowIsFinish({
				bizUuid: this.prvParam.bizUuid
			})
				.then((res) => {
					this.$modal.hideLoading();
					if (res.success) {
						if (!this.auxiliaryIsEdit) {
							debugger
							// 	是否是副局长审批-最后一步（只有特殊修复有副局长审批此步骤再519已判断）
							this.handleChange(this.auxiliaryParams.approveMemoResult);
						} else if (!this.responsibleIsEdit && this.baseInfo.repairType !== '04') {
							// 	是否是非特殊修复的负责人审批（非特殊修复最后一步）
							debugger
							this.handleChange(this.responsibleParams.approveMemoResult);
						} else {
							if (!res.bean) {
								// 是否流程最后一步
								this.toSelectPerson(this.prvParam.bizUuid);
							} else {
								debugger
								this.handleChange(this.departmentParams.approveMemoResult);
							}
						}
					} else {
						this.$u.toast(res.msg);
					}
				})
				.catch((e) => {
					this.$modal.hideLoading();
					this.$u.toast('请求失败' + e);
				});
		},
		// 选择审批人
		toSelectPerson(docInsUuid = '') {
			this.$u.route({
				url: '/pages/commonPage/flowUserSelectComponent',
				params: {
					flowFormCode: this.prvParam.gfCode, // 日计划终止的gfcode
					docInsUuid: docInsUuid, // PC 端没传
				},
			});
		},
		// 退回处理 & 审批通过
		handleChange(approveResult = '03', nextPersonUuids = '') {
			let data = {
				approveMemo: this.departmentParams.approveMemo,
				approveMemoResult: this.departmentParams.approveMemoResult, // 01 同意 02 不同意
				approveResult: approveResult, // 退回
				bizUuid: this.prvParam.bizUuid,
				menuCode: '',
				nextPersonUuids: nextPersonUuids,
				processDefKey: this.prvParam.gfCode
			};
			if (!this.departmentIsEdit) {
            data.approveMemo = this.departmentParams.approveMemo;
            data.approveMemoResult = this.departmentParams.approveMemoResult;
            // data.approveResult = this.departmentParams.approveMemoResult;
          } else if (!this.responsibleIsEdit) {
            data.approveMemo = this.responsibleParams.approveMemo;
            data.approveMemoResult = this.responsibleParams.approveMemoResult;
			// data.approveResult = this.responsibleParams.approveMemoResult;
          } else if (!this.auxiliaryIsEdit) {
            data.approveMemo = this.auxiliaryParams.approveMemo;
            data.approveMemoResult = this.auxiliaryParams.approveMemoResult;
            // data.approveResult = this.auxiliaryParams.approveMemoResult;
          }
			this.$modal.showLoading('加载中...');
			creditService.auditCreditRepairRecord(data)
				.then((res) => {
					this.$modal.hideLoading();
					if (res.success) {
						this.backPrev({
							paramKey: 'refresh',
							param: true
						});
					} else {
						this.$u.toast(res.msg);
					}
				})
				.catch((e) => {
					this.$modal.hideLoading();
					this.$u.toast('提交失败' + e);
				});
		},
		changeHeight() {
			this.$nextTick(() => {
				this.$refs.cons.init();
				this.$refs.files.init();
			});
		},
		handleChangeRadio() {
			if (this.departmentParams.approveMemoResult == '01') {
				this.departmentParams.approveMemo = '同意'
			} else {
				this.departmentParams.approveMemo = '不同意'
			}
		},
		handleChangeRadioRe() {
			if (this.responsibleParams.approveMemoResult == '01') {
				this.responsibleParams.approveMemo = '准予修复'
			} else {
				this.responsibleParams.approveMemo = '不予修复'
			}
		},
		handleChangeRadioAu() {
			if (this.auxiliaryParams.approveMemoResult == '01') {
				this.auxiliaryParams.approveMemo = '准予修复'
			} else {
				this.auxiliaryParams.approveMemo = '不予修复'
			}
		},
		updateTableHeight() {
			commonService.goFiles(this.prvParam.bizUuid, this.$FILE_GROUP_TYPE.m_credit_apply).then((rs) => {
				if (rs.success) {
					if (rs.data.length > 0) {
						rs.data.forEach(item => {
							if (typeof item.gfiSize == 'number') {
								item.gfiSize = this.getfilesize(item.gfiSize)
							}
						})
						this.dataSource = rs.data;
						setTimeout(() => {
							this.scrollRealHeight = this.$refs.tbRef.$el.clientHeight + 5 + 'px';
						}, 100);
					}
				} else {
					this.$u.toast(rs.msg);
				}
			}).catch((err) => {
				this.$modal.hideLoading();
				this.$u.toast(err);
			});
		},
		getfilesize(size) {
			if (!size)
				return "";
			var num = 1024.00; //byte
			if (size < num)
				return size + "B";
			if (size < Math.pow(num, 2))
				return (size / num).toFixed(2) + "K"; //kb
			if (size < Math.pow(num, 3))
				return (size / Math.pow(num, 2)).toFixed(2) + "M"; //M
			if (size < Math.pow(num, 4))
				return (size / Math.pow(num, 3)).toFixed(2) + "G"; //G
			return (size / Math.pow(num, 4)).toFixed(2) + "T"; //T
		},
		getCurrStatus() {
			creditService.getFlowNodeStep({
				bizUuid: this.prvParam.bizUuid,
				processDefKey: this.prvParam.gfCode
			}).then((res) => {
				if (res.success) {
					this.step = res.bean.step;
					this.approveDis = false;
					if (this.step == 'view') {
						this.approvalDisabled = true;
					} else if (this.step == 'edit') {
						this.ifSub = true;
					}
				}
			});
			creditService.getApproves({
				bizUuid: this.prvParam.bizUuid,
				processDefKey: this.prvParam.gfCode
			}).then((res) => {
				// 如果start 没有 那承办人意见不可编辑
				if (!res.bean.start) {
					this.undertakerIsEdit = true;
				} else {
					this.undertakerIsEdit = !res.bean.start.isCurryApproveNode;
				}

				let departmentParams = res['bean']['01'];
				// 承办部门意见有值
				this.departmentParams.approveMemoResult = departmentParams.approveResult || '01';
				this.departmentParams.approveMemo = departmentParams.approveMemo || '同意';
				this.departmentParams.handlePerson = departmentParams.approvePersonName || '';
				this.departmentParams.applyDate = departmentParams.approveTime || '';
				this.departmentIsEdit = !departmentParams.isCurryApproveNode;

				// 负责人
				let responsibleParams = res.bean.fzr;
				this.responsibleParams.approveMemoResult = responsibleParams.approveResult || '01';
				this.responsibleParams.approveMemo = responsibleParams.approveMemo || '准予修复';
				this.responsibleParams.handlePerson = responsibleParams.approvePersonName || '';
				this.responsibleParams.applyDate = responsibleParams.approveTime || '';
				this.responsibleIsEdit = !responsibleParams.isCurryApproveNode;

				if (res.bean.fjz) {
					// 副局长
					let auxiliaryParams = res.bean.fjz;
					this.auxiliaryParams.approveMemoResult = auxiliaryParams.approveResult || '01';
					this.auxiliaryParams.approveMemo = auxiliaryParams.approveMemo || '准予修复';
					this.auxiliaryParams.handlePerson = auxiliaryParams.approvePersonName || '';
					this.auxiliaryParams.applyDate = auxiliaryParams.approveTime || '';
					this.auxiliaryIsEdit = !auxiliaryParams.isCurryApproveNode;
				}

			});
		},
		updateBackState(param) {
			if (param.flowUserSelectBack) {
				// 选择审批人
				let nextPersons = [];
				for (let fu of param.flowUserSelectBack) {
					nextPersons.push(fu.personId);
				}
				if (this.ifSub) {
					this.stopPlan(nextPersons.join(',')); // 提交选人
				} else {
					this.handleChange('01', nextPersons.join(',')); // 审批通过
				}
			}
		},
		stopPlan(persons) {
			if (!persons) {
				this.$u.toast('请选择人员！');
				return;
			}
			let params = Object.assign({}, this.baseInfo)
			params.nextPersonUuids = persons;
			params.status = '01';
			this.$modal.showLoading('正在提交...');
			creditService
				.saveLicCreditRepairRecord(params)
				.then((res) => {
					this.$modal.hideLoading();
					if (res.success) {
						this.backPrev({
							paramKey: 'refresh',
							param: true
						});
					} else {
						this.$u.toast(res.msg);
					}
				})
				.catch((e) => {
					this.$modal.hideLoading();
					this.$u.toast(e.msg);
				});
		},
		initData() {
			this.$modal.showLoading('加载中...');
			creditService.getCreditRepairRecord({repairRecordUuid:this.prvParam.bizUuid})
				.then((res) => {
					this.$modal.hideLoading();
					if (res.success) {
						this.bean = res.bean;
						this.baseInfo = res.bean;
						this.getCurrStatus();

						this.endTitle = this.bean.status == M_DAY_PLAN_STATUS.TERMINATION ? '结束' : '...';
						this.updateTableHeight(); // 附件上传表格 高度							
					} else {
						this.$modal.alert(res.msg, () => {
							this.error();
						});
					}
				})
				.catch((e) => {
					this.$modal.hideLoading();
					this.$modal.alert('加载失败' + e, () => {
						this.error();
					});
				});
		},
		getGapMarginRowNumber() {
			return '-' + this.$theme.cm_content_paddng_row.replace('rpx', '');
		}
	},
	error() {
		uni.navigateBack();
	},
};
</script>

<style scoped lang="scss">
.page {
	padding: 0;
}

.sub-title {
	background: #f5f5f5;
	color: #989ea1;
	font-size: 24rpx;
	border-radius: 4px;
	margin-right: 10px;
	padding: 4px 8px;
}

.f-ctn {
	width: 100%;
}

.tb-lay {
	width: 900px;
	margin-left: 1px;
	margin-right: 1px;
}

.content {
	padding-left: $cm-content-paddng-row;
	padding-right: $cm-content-paddng-row;
	padding-top: $cm-conent-padding-column;
	padding-bottom: 70px;
}

.td-text {
	word-break: break-all;
	word-wrap: break-word;
}

/deep/.u-td {
	height: auto;
}
</style>